<?php

namespace App\Admin\Actions\ShopProduct;

use App\Library\Logic\OperateLogLogic;
use App\Library\Models\AdminShopProductModel;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;

class ShopProductSetAgentAction extends RowAction
{
    public function __construct(int $status = 0)
    {
        parent::__construct();
        $this->name = !$status ? '设置代理商商品' : '取消代理商商品';
    }

    public function handle(AdminShopProductModel $model)
    {
        $isAgent = (int)$model->is_agent ? 0 : 1;
        $model->is_agent = $isAgent;
        $model->save();
        OperateLogLogic::add(Admin::user()->user_id, 'admin.product.setProductAgentStatus', ['is_agent' => $isAgent]);
        return $this->response()->success('操作成功')->refresh();
    }
}
